﻿package motor.grafico {
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	
	public class GraficoAnimado extends Grafico
	{
		
		static public var ANIMACION_MAX_X:int = 32;
		static public var ANIMACION_MAX_Y:int = 32;
		
		static public var FRAME_CUAD:int = 5; //5x5 = 25 cuadros
		
		private var currentFrame:int;
		private var currentBitmapData:BitmapData;
		
		public function GraficoAnimado(unEscenario:Sprite)
		{
			super (unEscenario);
			currentFrame = 0;			
		}
		
		override protected function getDefaultBitmap():Bitmap
		{
			currentBitmapData = new BitmapData (ANIMACION_MAX_X,ANIMACION_MAX_Y);
			return new Bitmap(currentBitmapData);
		}
		
		protected function getBitmapDataAnimation():BitmapData
		{
			return Graficos.getInstancia().getBitmapDataExplosion1();
		}
		
		protected function paintFame (frameNumber:int)
		{
			var cuadX:int = frameNumber % FRAME_CUAD;
			var cuadY:int = frameNumber / FRAME_CUAD;
			//----
			var sourceX:int=ANIMACION_MAX_X*cuadX;
			var sourceY:int=ANIMACION_MAX_Y*cuadY;
			//----
			var bitmapDataSource:BitmapData = getBitmapDataAnimation();
			currentBitmapData.copyPixels(bitmapDataSource, new Rectangle(sourceX,sourceY,ANIMACION_MAX_X,ANIMACION_MAX_Y),new Point(0,0));
		}
		
		override public function animar (e:Event)
		{
			super.animar(e);
			paintFame(currentFrame);
			currentFrame++;
			if (currentFrame==FRAME_CUAD*FRAME_CUAD)
			{
					currentFrame=0;
			}
		}
		
	}
	
}